package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.config.response.JsonResult;
import cn.tedu._02notice.base.config.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeListQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVo;
import cn.tedu._02notice.pojo.vo.NoticeListVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//@Controller
@Slf4j
@RestController
@Tag(name = "01-咨询管理模块")
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/add")
//    @ResponseBody
    @Operation(summary = "添加咨询")
    @ApiOperationSupport(order = 10)
    @Parameters({
            @Parameter(name = "title",description = "标题"),
            @Parameter(name = "content",description = "内容"),
            @Parameter(name = "type",description = "类型"),
            @Parameter(name = "status",description = "状态"),
            @Parameter(name = "noticeAddPatam",hidden = true)
    })
    public JsonResult add(NoticeAddParam noticeAddParam) {
        System.out.println("noticeAddParam" + noticeAddParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam, notice);

        notice.setUserId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        noticeMapper.insertNotice(notice);

        return JsonResult.ok();
    }

    @GetMapping("/list")
    @Operation(summary = "查询列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(NoticeListQuery noticeListQuery) {
        System.out.println("list" + noticeListQuery);

        List<NoticeListVo> noticeListVos = noticeMapper.listNotice(noticeListQuery);

        return JsonResult.ok(noticeListVos);
    }

    @GetMapping("/detail")
    @Operation(summary = "查询咨询")
    @ApiOperationSupport(order = 30)
    @Parameter(name="noticeId",description = "咨询ID",required=true,example = "1")
    public JsonResult detail(Long noticeId) {
        System.out.println("noticeId" + noticeId);

        NoticeDetailInfoVo noticeDetailInfoVo = noticeMapper.selectNotice(noticeId);

        return JsonResult.ok(noticeDetailInfoVo);
    }

    @DeleteMapping("/delate")
    @Operation(summary = "删除咨询")
    @ApiOperationSupport(order = 40)
    public JsonResult del(Long noticeId) {
        System.out.println("noticeId" + noticeId);

        int i = noticeMapper.deleteById(noticeId);

        if (i > 0) {
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @PostMapping("/update")
    @Operation(summary = "修改咨询")
    @ApiOperationSupport(order = 50)
    public JsonResult update(@RequestBody NoticeUpdateParam noticeUpdateParam) {
        System.out.println("noticeUpdateParam" + noticeUpdateParam);

        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);

        notice.setUpdateTime(new Date());

        int i = noticeMapper.updateById(notice);

        if (i > 0) {
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

    }
}
